import types

from django import template
from django.conf import settings

register = template.Library()

def getattribute(value, arg):
    """Gets an attribute of an object dynamically from a string name"""
    
    if hasattr(value, arg):
        obj = getattr(value, arg)
        if type(obj) == types.FunctionType:
            return obj()
        return obj 
    elif type(value)==dict and value.has_key(arg):
        return value[arg]
    else:
        return settings.TEMPLATE_STRING_IF_INVALID

register.filter('getattr', getattribute)